package io.renren.modules.substation.controller;

import java.util.*;

import io.renren.modules.substation.entity.SelectItemInfo;
import io.renren.modules.substation.entity.TBaseDictInfoEntity;
import io.renren.modules.substation.entity.TPointInfoDataConfEntity;
import io.renren.modules.substation.iSendMsg;
import io.renren.modules.substation.service.TBaseDictInfoService;
import io.renren.modules.substation.service.TPointInfoDataConfService;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.socket.InitPointInfoDataConfService;
import io.renren.socket.SendMsgToClent;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.substation.entity.TLinkageConfEntity;
import io.renren.modules.substation.service.TLinkageConfService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 系统联动表
 *
 * @author yzx
 * @email sunlightcs@gmail.com
 * @date 2020-07-24 23:45:06
 */
@RestController
@RequestMapping("substation/tlinkageconf")
public class TLinkageConfController extends AbstractController {
    @Autowired
    private TLinkageConfService tLinkageConfService;
    @Autowired
    private TBaseDictInfoService tBaseDictInfoService;
    @Autowired
    protected TPointInfoDataConfService tPointInfoDataConfService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("substation:tlinkageconf:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = tLinkageConfService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 联动类型列表
     */
    @RequestMapping("/getTypeConditionsList")
    @RequiresPermissions("substation:tlinkageconf:getTypeConditionsList")
    public R getTypeConditionsList(@RequestParam Map<String, Object> params){
        List<SelectItemInfo> retList = new ArrayList<SelectItemInfo>();
        SelectItemInfo temp0=new SelectItemInfo("--请选择--","");
        retList.add(temp0);

        List<TBaseDictInfoEntity> dic_List = tBaseDictInfoService.queryByDictKey("TYPE_CONDITIONS");
        for(int i=0;i<dic_List.size();i++) {
            SelectItemInfo temp=new SelectItemInfo(dic_List.get(i).getDictDesc(),dic_List.get(i).getDictValue());
            retList.add(temp);
        }

        return R.ok().put("list", retList);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{logId}")
    @RequiresPermissions("substation:tlinkageconf:info")
    public R info(@PathVariable("logId") Long logId){
		TLinkageConfEntity tLinkageConf = tLinkageConfService.getById(logId);

        return R.ok().put("tLinkageConf", tLinkageConf);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("substation:tlinkageconf:save")
    public R save(@RequestBody TLinkageConfEntity tLinkageConf){
        SysUserEntity sysUserEntity = getUser();
        tLinkageConf.setCreateOpr(sysUserEntity.getUserId());
        tLinkageConf.setCreateTime(new Date());
        tLinkageConf.setLastOpr(sysUserEntity.getUserId());
        tLinkageConf.setLastTime(new Date());

        if(tLinkageConf.getPerformConditions()!=null && !"".equals(tLinkageConf.getPerformConditions())){
            List<TPointInfoDataConfEntity> list = InitPointInfoDataConfService.cache.get("Point");
            for(int i=0;i<list.size();i++){
                if(tLinkageConf.getPerformDevCode().equals(list.get(i).getDevCode()) &&
                        tLinkageConf.getPerformAdd().equals(list.get(i).getAddPublic()) &&
                        tLinkageConf.getPerformDesc().equals(list.get(i).getStatusTypeDesc()))
//                                && tLinkageConf.getPerformValue().equals(list.get(i).getStatusTypeDesc())))
                {
                    TPointInfoDataConfEntity tPointInfoDataConfEntity = new TPointInfoDataConfEntity();
                    tPointInfoDataConfEntity.setLastOpr((long) 0);
                    tPointInfoDataConfEntity.setLastTime(new Date());
                    tPointInfoDataConfEntity.setAddPublic(tLinkageConf.getPerformAdd());
                    tPointInfoDataConfEntity.setAddress(list.get(i).getAddress());
                    tPointInfoDataConfEntity.setDevCode(tLinkageConf.getPerformDevCode());
                    tPointInfoDataConfEntity.setDataValue(tLinkageConf.getPerformValue());
                    tPointInfoDataConfEntity.setTypeFl(list.get(i).getTypeFl());
                    int updsum =  tPointInfoDataConfService.updateByAddAndType(tPointInfoDataConfEntity);
                    if (updsum <= 0) {
                        logger.debug("无执行数据匹配");
                        return R.ok().put("result","未配置执行点");
                    }
                }
            }
        }else{
            String result = iSendMsg.remoteControl104("2",tLinkageConf.getPerformDevCode(),tLinkageConf.getPerformAdd(),tLinkageConf.getPerformDesc(),tLinkageConf.getPerformValue(),null);
            if(result != null){
                logger.error("result:"+result);
            }
        }

		tLinkageConfService.save(tLinkageConf);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("substation:tlinkageconf:update")
    public R update(@RequestBody TLinkageConfEntity tLinkageConf){
        SysUserEntity sysUserEntity = getUser();
        tLinkageConf.setLastOpr(sysUserEntity.getUserId());
        tLinkageConf.setLastTime(new Date());

		tLinkageConfService.updateById(tLinkageConf);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("substation:tlinkageconf:delete")
    public R delete(@RequestBody Long[] logIds){
		tLinkageConfService.removeByIds(Arrays.asList(logIds));

        return R.ok();
    }

}
